package com.base.padding;

/**
* 注意：access_tocken最好复制而不是生成，否则会影响当前运行服务所使用的tocken
* 下载图片的补丁服务 用来解决个别图片没有下载的问题
*/

import java.awt.Color;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.*;

import tool.*;

import com.base.bean.*;
import com.base.service.*;
import com.base.dao.*;
import com.wechat.service.WechatService;


public class DownImgPadding {
	static String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
	
	ImageDao imageDao;
	
	public DownImgPadding() {
		// TODO Auto-generated constructor stub
		imageDao = (ImageDao) _BaseDao.ac.getBean("ImageDao");
		
	}
	
	public List<Image> testImgByPage(List<Image> images){
		ImageService is=ImageService.getInstance();
		
		
		for(Image img:images){
		try{
			String url="http://we.51waji.com/picture/"+img.getPath()+"/"+img.getFileName();
//			System.out.println(url);
			
			if(!ImageToolTwo.testDownload(url, 8)){
				System.out.println(img.getFileName()+"##"+img.getMediaId());
			}
			
		}catch(Exception e){
			System.out.println(img.getMediaId()+"@can't find");
			
		}
		}
		return images;
	}
	
	
	public List<Image> downImgByPage(List<Image> images){
		ImageService is=ImageService.getInstance();
		DownImgPadding dp=new DownImgPadding();
		
		for(Image img:images){
		try{
			System.out.print(img.getMediaId()+"##");
			System.out.println(img.getDiscription());
			Integer creator=img.getCreator();
			String mediaId=img.getMediaId();
			String memo=img.getDiscription();
			is.upload2(creator, mediaId, memo);
			
		}catch(Exception e){
			System.out.println(img.getMediaId()+"@can't find");
			
		}
		}
		return images;
	}
	
	private List<Image> getImgByPage(int offset,int pagesize){
		String hql="from Image";
		return imageDao.findByPage(hql, offset, pagesize);
	}
	
	private void rename(List<Image> images) throws IOException{
		String oldPath="D:\\_temp\\tmp\\2016\\2";
		String newPath="D:\\_temp\\tmp\\2016\\3";
		
		
		
		for(Image img:images){
			String file1=oldPath+"\\"+img.getMediaId()+".jpg";
			String file2=newPath+"\\"+img.getFileName()+".jpg";
			try{
			FileTool.copyFile(file1, file2);
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		
	}
	
	public  List<Image>  markAndMove(List<Image> images){
		String oldPath="D:\\_temp\\tmp\\2016\\1";
		String newPath="D:\\_temp\\tmp\\2016\\2";
		
		for(Image image:images){
		
			
		
		
		image.setCreated(image.getCreated());
		
		
		//String timeStr=TimeTool.getNowTime()+"  "+TimeTool.getWeekDay();
		long t=image.getCreated().getTime();
		DateUtil du=new DateUtil();
		du.setTime(t);
		String r=du.getDateString();
		String time=du.getTimeString();
		int i=du.getWeek();
		String xq=this.weekDays[i-1];
		
		String timeStr=r+" "+time+" "+xq;
		try{
		WaterMarkUtils.getInstance().mark(oldPath+"\\"+image.getMediaId()+".jpg", newPath+"\\"+image.getMediaId()+".jpg", Color.red, image.getDiscription(),timeStr);
		}catch(Exception e){
			e.printStackTrace();
		}
		
		image.setState(1);		//重命名水印后的文件到本地路径
		}
		return images;
	}
	
	
	public  List<Image>  markAndMove2(List<Image> images){
		String oldPath="D:\\_temp\\tmp\\2016\\1";
		String newPath="D:\\_temp\\tmp\\2016\\2";
		
		for(Image image:images){
		
			
		
		
		image.setCreated(image.getCreated());
		
		
		//String timeStr=TimeTool.getNowTime()+"  "+TimeTool.getWeekDay();
		long t=image.getCreated().getTime();
		DateUtil du=new DateUtil();
		du.setTime(t);
		String r=du.getDateString();
		String time=du.getTimeString();
		int i=du.getWeek();
		String xq=this.weekDays[i-1];
		
		String timeStr=r+" "+time+" "+xq;
		try{
		WaterMarkUtils.getInstance().mark(oldPath+"\\"+image.getMediaId()+".jpg", newPath+"\\"+image.getMediaId()+".jpg", Color.red, image.getDiscription(),timeStr);
		}catch(Exception e){
			e.printStackTrace();
		}
		
		image.setState(1);		//重命名水印后的文件到本地路径
		}
		return images;
	}
	
	
	
	/**
	 * 注意：access_tocken最好复制而不是生成，否则会影响当前运行服务所使用的tocken
	 * 下载图片的补丁服务 用来解决个别图片没有下载的问题
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		

		
		
		
		//03-22 am     20??
		List<Image> images=null;
		DownImgPadding dp=new DownImgPadding();
		
		images=dp.getImgByPage(3100,100);		//从image表中按页湖区
//		dp.testImgByPage(images);	//测试下载图片
		dp.downImgByPage(images);    // 按页下载图片 
		
		//打水印
		
		//dp.markAndMove(images);
		
//		dp.rename(images);		//重命名
		
		
		
	}

}
